package com.nervenets.web.controller.handler.user.user;

import com.nervenets.web.controller.Global;
import com.nervenets.web.controller.handler.Handler;
import com.nervenets.web.entity.MessageCode;
import com.nervenets.web.hibernate.domain.user.User;

import javax.servlet.http.HttpServletRequest;

public class UserResetPwdHandler extends Handler<UserResetPwdParams> {
    @Override
    public String handle(UserResetPwdParams params, HttpServletRequest request) throws Exception {
        if (!params.getVCode().equals(globalService.getGlobalPair(String.format(Global.Constants.MOBILE_VALIDATION_CODE, params.getMobile())))) {
            return errorMessage(MessageCode.code_444, "您填写的验证码不正确或失效！");
        }

        if (!params.getPassword().equals(params.getPasswordTry())) {
            return errorMessage(MessageCode.code_444, "两次输入的密码不一致！");
        }

        if (!(params.getPassword().length() >= 6 && params.getPassword().length() <= 20)) {
            return errorMessage(MessageCode.code_444, "请输入6-20位密码");
        }

        User userByMobile = userDao.getUserByMobile(params.getMobile());
        if (null == userByMobile) errorMessage(MessageCode.code_444, "该手机号码未注册！");

        userByMobile.setPassword(params.getPassword());
        userDao.saveOrUpdate(userByMobile);

        //userDao.saveOrUpdate(new Message(userByMobile.getId(), TagType.system, "恭喜您，密码修改成功！", "", UnionType.normal));

        return successMessage();
    }

    @Override
    public String result() throws Exception {
        return successMessage();
    }
}
